import allure
from selenium.common import NoSuchElementException
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By

from base.base_page import BasePage

from utils.logger import logger


class HomePage(BasePage):
    # 侧边栏订单管理的下拉框状态
    ORDER_FORM_STATUS = 0
    _ORDER_FORM = (By.XPATH, "//span[text()='订单管理']")
    _ORDER_LIST = (By.XPATH, "//*[text()='订单列表']")
    _ORDER_ENTRY = (By.XPATH, "//*[text()='订单录入']")
    _TEMPLATE_MANAGE = (By.XPATH, "//*[text()='模板管理']")
    _OK = (By.XPATH, '//*[contains(text(),"确 定")]')

    def click(self):
        if len(self._driver.find_elements(By.CSS_SELECTOR, ".el-icon-close.inline-block.absolute")) > 0:
            self._driver.find_element(By.CSS_SELECTOR, ".el-icon-close.inline-block.absolute").click()

        if len(self.finds(self._OK)) > 0:
            # self.set_time(3)
            self.find_and_click(self._OK)
        return self

    @allure.step("打开订单管理下拉框")
    def open_order_form(self):
        logger.info('打开订单管理下拉框')
        # 如果已经打开了，那就直接返回
        if self.ORDER_FORM_STATUS == 0:
            self.ORDER_FORM_STATUS = 1
        else:
            return
        self.find_and_click(self._ORDER_FORM)
        return

    _GODDS = (By.XPATH, "//*[text()='商品管理']")
    _GOODS_LIST = (By.XPATH, "//*[text()='商品列表']")

    @allure.step("打开商品管理下拉框")
    def open_goods_man(self):
        logger.info('打开商品管理下拉框')
        # 如果已经打开了，那就直接返回
        if self.ORDER_FORM_STATUS == 0:
            self.ORDER_FORM_STATUS = 1
        else:
            return
        self.find_and_click(self._GODDS)
        return

    @allure.step("打开仓库供应商下拉框")
    def open_ware_form(self):
        logger.info('打开仓库供应商下拉框')
        # 如果已经打开了，那就直接返回
        if self.ORDER_FORM_STATUS == 0:
            self.ORDER_FORM_STATUS = 1
        else:
            return
        self.find_and_click(self._WARE)
        return

    # -----------------------------------------------------------------------------------
    @allure.step("打开模板管理下拉框")
    def open_template_manage(self):
        logger.info('打开模板管理下拉框')
        # 如果已经打开了，那就直接返回
        if self.ORDER_FORM_STATUS == 0:
            self.ORDER_FORM_STATUS = 1
        else:
            return
        self.find_and_click(self._TEMPLATE_MANAGE)
        return

    # -----------------------------------------------------------------------------------
    _DELIVERY_TEMP = (By.XPATH, "//*[text()='发货模板']")

    @allure.step("前往发货模板页面")
    def goto_delivery_template(self):
        logger.info("前往发货模板页面")
        self.open_template_manage()
        self.finds(self._DELIVERY_TEMP)[1].click()
        from page.Delivery_temp_page import DeliveryTempPage
        return DeliveryTempPage(self._driver)

    # -----------------------------------------------------------------------------------

    _CHANNEL_TEMP = (By.XPATH, "//*[text()='渠道模板']")

    @allure.step("前往渠道模板页面")
    def goto_channel_template(self):
        logger.info("前往渠道模板页面")
        self.open_template_manage()
        self.finds(self._CHANNEL_TEMP)[1].click()
        from page.channel_temp_page import ChannelTempPage
        return ChannelTempPage(self._driver)

    # -----------------------------------------------------------------------------------

    _FREIGHT_TEMP = (By.XPATH, "//*[text()='运费模板']")

    @allure.step("前往运费模板页面")
    def goto_freight_template(self):
        logger.info("前往运费模板页面")
        self.open_template_manage()
        self.find_and_click(self._FREIGHT_TEMP)
        from page.freight_temp_page import FreightTempPage
        return FreightTempPage(self._driver)

    # -----------------------------------------------------------------------------------

    @allure.step("前往订单列表页面")
    def goto_order_form_page(self):
        logger.info("前往订单列表页面")
        self.open_order_form()
        self.find_and_click(self._ORDER_LIST)
        from page.orderform_page import OrderformPage
        return OrderformPage(self._driver)

    @allure.step("前往订单录入页面")
    def goto_order_entry(self):
        logger.info("前往订单录入页面")
        self.open_order_form()
        self.find_and_click(self._ORDER_ENTRY)
        from page.order_entry_page import OrderEntryPage
        return OrderEntryPage(self._driver)

    _WARE = (By.XPATH, '//*[text()="仓库(供应商)管理"]')
    _WARE_MAN = (By.XPATH, '//*[text()="仓库(供应商)"]')

    @allure.step("前往仓库管理页面")
    def goto_ware_man(self):
        logger.info("前往仓库管理页面")
        self.open_ware_form()
        self.find_and_click(self._WARE_MAN)
        from page.ware_man_page import WareManPage
        return WareManPage(self._driver)

    @allure.step("前往商品列表页面")
    def goto_goods_list(self):
        logger.info("前往商品列表页面")
        self.open_goods_man()
        self.find_and_click(self._GOODS_LIST)
        from page.goods_list_page import GoodsListPage
        return GoodsListPage(self._driver)
